<template>
  <basic-dialog
    width="60%"
    title="选择替代商品"
    :visible="visible"
    :showFooter="false"
    :handleClose="handleCloseDialog"
  >
    <BasicForm
      ref="basicForm"
      :isScalingBtn="false"
      :hasQueryPermission="false"
      :formColumns="formItemList"
      :dataSource="formData"
      :handleSearchForm="handleQueryForm"
      @getFormData="getFormData"
    />
    <BasicTable
      showActionCol
      :showSelectCol="false"
      :loading="tableInfo.loading"
      :columns="tableInfo.columns"
      :dataSource="tableInfo.list"
      :pagination="pagination"
      :pageSizes="pageSizes"
      :tableHeight="480"
      :actionRender="renderTableBtnAction"
      @pageChange="handlePageChange"
      @pageSizeChange="handlePageSizeChange"
    />
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicForm from '@/components/BasicForm';
import BasicTable from '@/components/BasicTable';
import basePage from '@/mixins/commodity/basePage';
import { getTableAction } from '@/utils/getBtnsRender';
import { searchReplaceProductsService } from '@/api/commodity/common';
export default {
  mixins: [basePage],
  components: {
    BasicDialog,
    BasicForm,
    BasicTable
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    productKind: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      formData: {
        spuNo: '', // spu编码
        skuNo: '', // sku编码
        productName: '', // 商品名称
      },
      formItemList: [
        { label: 'SPU编码', prop: 'spuNo', type: 'input' },
        { label: 'SKU编码', prop: 'skuNo', type: 'input' },
        { label: '商品名称', prop: 'productName', type: 'input' }
      ],
      tableInfo: {
        list: [],
        columns: [
          { title: 'SPU编码',  key: 'spuNo' },
          { title: 'SKU编码',  key: 'skuNo' },
          { title: '商品名称',  key: 'productName' },
          { title: '规格',  key: 'specDetail' },
          { title: '单位名称',  key: 'unitName' }
        ],
        loading: false
      }
    }
  },
  watch: {
    visible (newVal) {
      newVal && this.getTableData();
    }
  },
  methods: {
    // 渲染表格按钮
    renderTableBtnAction (h, { row }) {
      const actionList = [{ label: '选择', method: this.handleSelect }];
      return getTableAction({ h, row, actionList })
    },
    // 获取表格数据
    async getTableData () {
      const data = { ...this.formData, ...this.paginationParams, productKind: this.productKind };
      const loadingFn = state => { this.tableInfo.loading = state; }
      const retData = await searchReplaceProductsService(data, loadingFn);
      if (retData) {
        this.tableInfo.list = retData?.results ?? [];
        this.pagination.totalNum = retData?.totalNum ?? 0;
      }
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
    // 选择
    handleSelect (row) {
      this.$emit('getResults', row);
      this.$emit('update:visible', false);
      this.$refs.basicForm.handleResetForm();
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .el-dialog__header {
  border-bottom: 1px solid #e8eaec;
}
/* 分页样式 */
.pagination_wrap {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
